package com.laijiale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laijiale.Service.DeptService;
import com.laijiale.dto.*;
import com.laijiale.entities.Dept;
import com.laijiale.entities.Room;
import com.laijiale.excel.DeptExcel;
import com.laijiale.excel.RoomExcel;
import com.laijiale.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:59
 */
@Tag(name = "部门控制器")
@RestController
@RequestMapping("/api/v1/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Operation(summary = "添加一条记录")
    @PostMapping("save")
    public Object save(@RequestBody  @Validated DeptInsertDTO deptInsertDTO){

        return deptService.insert(deptInsertDTO);
    }
    @Operation(summary = "查询一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return deptService.selectById(id);
    }
    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated DeptUpdateDTO deptUpdateDTO){

        return deptService.updateById(deptUpdateDTO);

    }
    @Operation(summary = "删除一条记录")
    @PostMapping("remove/{id}")
    public Object remove(@PathVariable Long id){

        return deptService.logicDeleteById(id);

    }
    @Operation(summary = "批量删除记录")
    @PostMapping("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){

        return deptService.logicDeleteByIds(ids);

    }
    @Operation(summary = "全查询记录")
    @GetMapping("list")
    public Object list(){

        return deptService.list();

    }
    
    @Operation(summary = "分页查询记录")
    @PostMapping("page")
    public Object page(@RequestBody  @Validated PageDTO pageDTO){

        return deptService.page(pageDTO);

    }
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<DeptExcel> deptExcels = new ArrayList<>();
        List<Dept> depts = deptService.list();
        for (Dept dept: depts) {
            DeptExcel deptExcel = BeanUtil.copyProperties(dept, DeptExcel.class);
            deptExcels.add(deptExcel);
        }
        EasyExcelUtil.download(resp, "部门统计表.xlsx", "sheet01",
                DeptExcel.class, deptExcels);
    }
    @Operation(summary = "按部门名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated DeptPageDTO dto){
        return deptService.pageLikeTitle(dto);
    }



}
